<?php
/* *
 * $Author ：PHPYUN开发团队
 *
 * 官网: http://www.phpyun.com
 *
 * 版权所有 2009-2018 宿迁鑫潮信息技术有限公司，并保留所有权利。
 *
 * 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class jobadd_controller extends adminCommon{
    function set_search(){
        include PLUS_PATH."/com.cache.php";
        foreach($comdata['job_type'] as $k=>$v){
            $comarr[$v]=$comclass_name[$v];
        }
        foreach($comdata['job_salary'] as $k=>$v){
            $comar[$v]=$comclass_name[$v];
        }
        include(CONFIG_PATH."db.data.php");
        $source=$arr_data['source'];
        $this->yunset('source',$source);
        $search_list[]=array("param"=>"entry_state","name"=>'简历状态',"value"=>array("0"=>"未面试","1"=>"待入职","2"=>"不合格","3"=>"已入职","4"=>"已离职"));
        $this->yunset("search_list",$search_list);
    }
	function index_action(){
        $this->cheakjava();
        $key = $_GET['key'];
        $this->yunset("key",$key);
        //todo 公司信息进行获取
        include(CONFIG_PATH."db.data.php");
        $this->yunset("arr_data",$arr_data);
          $CacheArr=$this->MODEL('cache')->GetCache(array('hy','job','city','com','circle'));
        $this->yunset($CacheArr);
        //2019/4/16 替换跨库为java接口
//        $company_info = $this->objlan->DB_select_all("t_enterprise");
        $yingpin_model = $this->MODEL('yingpin');
        $company_info = $yingpin_model->getCompany();

        $this->yunset("company_info",$company_info);
        $row['sdate']=mktime();
        //$row['number']=$CacheArr['comdata']['job_number'][0];
        $row['type']=$CacheArr['comdata']['job_type'][0];
        $row['exp']=$CacheArr['comdata']['job_exp'][0];
        $row['report']=$CacheArr['comdata']['job_report'][0];
        $row['age']=$CacheArr['comdata']['job_age'][0];
        $row['edu']=$CacheArr['comdata']['job_edu'][0];
        $row['marriage']=$CacheArr['comdata']['job_marriage'][0];
        $this->yunset("row",$row);
        $this->yuntpl(array('admin/jobadd'));
	}
	function edit_action(){
        $this->cheakjava();
        $key = $_GET['key'];
        $this->yunset("key",$key);

        include(CONFIG_PATH."db.data.php");
        $this->yunset("arr_data",$arr_data);
        if($_GET['id']){
            $id=(int)$_GET['id'];
        }else{
            $this->ACT_layer_msg("不合法提交！",8);
        }
        //2019/4/16 替换跨库为java接口
//        $company_info = $this->objlan->DB_select_all("t_enterprise");
        $yingpin_model = $this->MODEL('yingpin');
        $company_info = $yingpin_model->getCompany();
//        if(!empty($company_info)){
//            foreach($company_info as $k=>$v){
//                if(!empty($v['enterprise_abbreviation'])){
//                    $company_info[$k]['enterprise_name'] = $v['enterprise_abbreviation'];
//                }
//            }
//        }

        $this->yunset("company_info",$company_info);
        $row=$this->obj->DB_select_once("company_job","`id`=".$id);
        $lang[] = @explode(',',$row['bright']);
        if($lang){
            foreach($lang as $key=>$val){
                $row['bright']=$val;
            }
        }
        $lang1[] = @explode(',',$row['welfare_other']);
        if($lang1){
            foreach($lang1 as $key1=>$val1){
                $row['welfare_other']=$val1;
            }
        }
        $this->yunset($this->MODEL('cache')->GetCache(array('hy','job','city','com','user')));
        if($row['three_cityid']){
            $row['circlecity']=$row['three_cityid'];
        }else if($row['cityid']){
            $row['circlecity']=$row['cityid'];
        }else if($row['provinceid']){
            $row['circlecity']=$row['provinceid'];
        }
        if($row['autotime']>time()){
            $row['autodate']=date("Y-m-d",$row['autotime']);
        }
        $CacheM=$this->MODEL('cache');
        $CacheArr=$CacheM->GetCache(array('city'));
        $address = $CacheArr['city_name'][$row['provinceid']].'省'.$CacheArr['city_name'][$row['cityid']].'市'.$CacheArr['city_name'][$row['three_cityid']].$row['address'];
        $this->yunset("row",$row);
        $this->yunset("page",$_GET['page']);
        $this->yunset("address",$address);
        $this->yunset("id",$id);
        $this->yunset("js_def",3);
        $this->yuntpl(array('admin/jobadd'));
	}
	function save_action(){
        $this->yunset("key",$_GET['key']);
        if( $_POST['submitBtn'] ){
            $data = array();
            $lang = @explode(",",$_POST['com_name']);
            $data['company_id'] = $lang[0];
            $data['com_name'] = $lang[1];
            $data['name'] = $_POST['name'];
            $data['nature'] = $_POST['nature'];
            if($_POST['job_post']){
                $row1=$this->obj->DB_select_once("job_class","`id`='".intval($_POST['job_post'])."'","`keyid`,`name`");
                $row2=$this->obj->DB_select_once("job_class","`id`='".$row1['keyid']."'","`keyid``name`");
                if($row2['keyid']=='0'){
                    $data['job1_son']=$_POST['job_post'];
                    $data['job1']=$row1['keyid'];
                    $data['job_name'] = $row1['name'];
                    $data['job_post']='';
                }else{
                    $data['job1_son']=$row1['keyid'];
                    $data['job_name']=$row1['name'];
                    $data['job1']=$row2['keyid'];
                    $data['job_post'] = $_POST['job_post'];
                }
            }
            $data['provinceid'] = $_POST['provinceid'];
            $data['description'] = str_replace(array("&amp;","background-color:#ffffff","background-color:#fff","white-space:nowrap;"),array("&",'background-color:','background-color:','white-space:'),$_POST['description']);
            $data['salarydetail'] = str_replace(array("&amp;","background-color:#ffffff","background-color:#fff","white-space:nowrap;"),array("&",'background-color:','background-color:','white-space:'),$_POST['salarydetail']);
            $data['cityid'] = $_POST['cityid'];
            $data['f_company'] = $_POST['f_company'];
            $data['three_cityid'] = $_POST['three_cityid'];
            $data['address'] = $_POST['address'];
            $data['x'] = $_POST['job_x'];
            $data['y'] = $_POST['job_y'];
            $data['number'] = $_POST['number'];
            $data['rest'] = $_POST['rest'];
            $data['minsalary'] = intval($_POST['minsalary']);
            $data['maxsalary'] = intval($_POST['maxsalary']);
            if($_POST['discuss']){
                $data['discuss'] = $_POST['discuss'];
            }
            $data['sunsalary'] = $_POST['sunsalary'];
            $data['nightsalary'] = intval($_POST['nightsalary']);
            $data['exp'] = $_POST['exp'];
            $data['age'] = $_POST['age'];
            $data['sex'] = $_POST['sex'];
            $data['edu'] = $_POST['edu'];
            $data['more_requirement'] = $_POST['more_requirement'];
            $data['welfare'] = $_POST['welfare'];
            $data['sdate'] = time();
            $data['state'] = 1;
            $data['status'] = 2;
            $data['salary_type'] = $_POST['salary_type'];
            $data['car'] = $_POST['car'];
            $data['wenshen'] = $_POST['wenshen'];
            $data['exam'] = $_POST['exam'];
            $data['idcard'] = $_POST['idcard'];
            $data['school_end'] = $_POST['school_end'];
            $data['return_factory'] = $_POST['return_factory'];
            $data['english'] = $_POST['english'];
            $data['maths'] = $_POST['maths'];
            $data['face'] = $_POST['face'];
            $data['dust'] = $_POST['dust'];
            $data['foreign_body'] = $_POST['foreign_body'];
            $data['food'] = $_POST['food'];
            $data['sleep'] = $_POST['sleep'];
            $data['trans'] = $_POST['trans'];
            $data['note'] = $_POST['note'];
            $data['salary_fafang'] = $_POST['salary_fafang'];
            $data['insurance'] = $_POST['insurance'];
            $data['tijian'] = $_POST['tijian'];
            $data['idcard_copy'] = $_POST['idcard_copy'];
            $data['school_end_copy'] = $_POST['school_end_copy'];
            $data['photos'] = $_POST['photos'];
            $data['minage'] = $_POST['minage'];
            $data['maxage'] = $_POST['maxage'];
            if(!isset($_POST['is_graduate'])){
                $data['is_graduate']=0;
            }else{
                $data['is_graduate']=$_POST['is_graduate'];
            }
            $data['bright']=@implode(',',$_POST['bright']);
            $data['welfare_other']=@implode(',',$_POST['welfare_other']);
            if( $_POST['id'] ){
                $id = $_POST['id'];
                unset($_POST['id'] );
                $res = $this->obj->update_once("company_job",$data,array('id'=>$id));
            }else{
                $res = $this->obj->insert_into("company_job",$data);
            }
            if( $res ){
                $this->ACT_layer_msg("发布成功！",9,$res);
            }
        }else{
            $this->ACT_layer_msg("不合法提交！",8);
        }
	}
	function showMemberList_action(){
        $this->set_search();
        if($_GET['id']){
            $id=(int)$_GET['id'];
            $this->yunset("id",$id);
        }else{
            $this->ACT_layer_msg("不合法提交！",8);
        }
        $wheres ='job_id = '.$id;
        if(trim($_GET['keyword'])!=""){
            $wheres .=" and  CONCAT(`member_name`,`id`,`member_phone`) like '%".trim($_GET['keyword'])."%'";
            $urlarr['keyword']=$_GET['keyword'];
        }
        if( isset($_GET['entry_state']) ){
            $wheres .=' and entry_state = '.$_GET['entry_state'];
            $urlarr['entry_state']=$_GET['entry_state'];
        }
        $urlarr['c']=$_GET['c'];
        $urlarr['id']=$_GET['id'];
        $urlarr['page']="{{page}}";
        $wheres .= ' order by `add_time` DESC';
        $pageurl=Url($_GET['m'],$urlarr,'admin');
        $rows=$this->get_page("entry",$wheres,$pageurl,$this->config['sy_listnum']);
        if(!empty($rows)){
            foreach($rows as $k=>$v){
                //2019/4/16 替换跨库为java接口
                $yingpin_model = $this->MODEL('yingpin');
                $agent = $yingpin_model->getUserRealname($v['agent_id']);

//                $agent = $this->dblan->DB_query_all("SELECT `realname` FROM agent a LEFT JOIN sys_user b ON a.`userInfo_id`=b.`user_id` WHERE a.`id`=".$v['agent_id']);
                $rows[$k]['agent'] =$agent['realname']?$agent['realname']:'无';
            }
        }
        include PLUS_PATH."job.cache.php";
        include PLUS_PATH."industry.cache.php";
        include PLUS_PATH."com.cache.php";
        $wheres=str_replace(array("(",")"),array("[","]"),$wheres);
        $this->yunset("wheres", $_GET['keyword']);
        $this->yunset("entry_state", $_GET['entry_state']);
        $this->yunset($this->MODEL('cache')->GetCache(array('job','hy')));
        $this->yunset("rows",$rows);
        $this->yuntpl(array('admin/jobadd_show_member_list'));
    }
    function showMember_action(){
    if($_GET['id']){
        $id=(int)$_GET['id'];
    }else{
        $this->ACT_layer_msg("不合法提交！",8);
    }
    $row=$this->obj->DB_select_once("entry","`id`=".$id);
    $row['member_sex'] = $row['sex'] == 1 ? '男' : '女';
    include PLUS_PATH."job.cache.php";
    include PLUS_PATH."industry.cache.php";
    include PLUS_PATH."com.cache.php";
    $this->yunset("row",$row);
    $this->yuntpl(array('admin/jobadd_show_member'));
}
    function del_action(){
//        $this->check_token();
        $data = array();
        $data['jobhits'] = array('jobhits',-1);
        if($_GET['del']||$_GET['id']){
            if(is_array($_GET['del'])){
                $layer_type=1;
                foreach ( $_GET['del'] as $k => $v ){
                    $row=$this->obj->DB_select_once("entry","`id`=".$v);
                    if( $row['job_id'] ){
                        $id = $row['job_id'];
                        $sql = "update `phpyun_company_job` set `jobhits` = IF(`jobhits`<1, 0, `jobhits`-1) WHERE `id` = $id ";
                        $res = $this->db->query( $sql );
                    }
                }
                $del=@implode(',',$_GET['del']);
            }else{
                $layer_type=0;
                $del=$_GET['id'];
                $row=$this->obj->DB_select_once("entry","`id`=".$del);
                if( $row['job_id'] ){
                    $id = $row['job_id'];
                    $sql = "update `phpyun_company_job` set `jobhits` = IF(`jobhits`<1, 0, `jobhits`-1) WHERE `id` = $id ";
                    $res = $this->db->query( $sql );
                }
                if( !$res ){
                    $this->layer_msg("删除失败！",8,1);
                }
            }
            //todo 公司信息进行修改
            $this->obj->DB_delete_all("entry","`id` in (".$del.")","");
            $this->layer_msg("简历(ID:".$del.")删除成功！",9,$layer_type,$_SERVER['HTTP_REFERER']);
        }else{
            $this->layer_msg("请选择您要删除的信息！",8,1);
        }
    }
    function checkState_action(){
        if( $_POST['pid'] ){
           $state=$_POST['entry_state'];
           $id = $this->obj->DB_update_all("entry","`entry_state`='".$state."',`edit_time`='".time()."'","`id`='".$_POST['pid']."'");
            $member_info = $this->obj->DB_select_once( 'entry', "`id`='".$_POST['pid']."'", '*' );
            $value_java = array();
            $value_java['userId'] = $member_info['member_id'];
            $value_java['workStatus']= "1";
            //2019/4/16 替换跨库为java接口
            $yingpin_model = $this->MODEL('yingpin');
            $yingpin_model->editBusinessOnce($value_java);
//            $this->objlan->DB_update_all("agent_business",$value_java,"`user_id`='".$member_info['member_id']."'");
           $id?$this->ACT_layer_msg("候选人状态(ID:".$_POST['pid'].")更改成功！",9,$_SERVER['HTTP_REFERER'],2,1):$this->ACT_layer_msg("设置失败！",8,$_SERVER['HTTP_REFERER']);
        }
    }

    function resume_action(){
        if($_POST['id']){
            $id=(int)$_POST['id'];
        }else{
            $this->ACT_layer_msg("不合法提交！",8);
        }
        set_time_limit(0);
        include LIB_PATH."/reader.php";
        $data = new Spreadsheet_Excel_Reader();
        $data->setOutputEncoding('utf-8');
        if($_FILES[excel][name]!=""){
            $time = time();
            $excel = $time.".xls";
            move_uploaded_file($_FILES[excel][tmp_name],DATA_PATH."upload/excel/".$excel);
        }else{
            $this->ACT_layer_msg("无文件上传！",8,$_SERVER['HTTP_REFERER'],2,1);
        }
        $data->read(DATA_PATH.'upload/excel/'.$excel);
        if($data->sheets[0]['numRows']<1){
            $this->ACT_layer_msg("数据读取失败，请检查excel格式！",8,$_SERVER['HTTP_REFERER'],2,1);
        }
        $user = array();
        $num = 0;
        $cells=count($data->sheets[0]['cells']);//行
        $cellsnum=count($data->sheets[0]['cells'][1]);//列
        for ($i = 2; $i <= $cells; $i++){
            $user[$i]['member_name'] 	 = trim($data->sheets[0]['cells'][$i][1]);
            $user[$i]['member_phone'] = trim($data->sheets[0]['cells'][$i][2]);
            $member_name = $user[$i]['member_name'];
            $member_phone = $user[$i]['member_phone'];
            if( !empty( $user[$i]['member_name'] ) && !empty( $user[$i]['member_phone'] ) ){
                $member_info = $this->obj->DB_select_once( 'entry', "`job_id` = '". $id ."' AND `member_name` = '". $member_name ."' AND `member_phone` = '". $member_phone ."' AND `entry_state` = '0'", '*' );
                if( $member_info ){
                    $value = '';
                    $value.="`entry_state`='1'";
                    $res = $this->obj->DB_update_all("entry",$value,"`id`='".$member_info['id']."'");
                    //2019/4/16 替换跨库为java接口
                    $value_java = array();
                    $value_java['userId'] = $member_info['member_id'];
                    $value_java['workStatus']="1";
                    $yingpin_model = $this->MODEL('yingpin');
                    $yingpin_model->editBusinessOnce($value_java);
//                    $res = $this->objlan->DB_update_all("agent_business",$value_java,"`user_id`='".$member_info['member_id']."'");
                    if( $res ){
                        $num = $num+1;
                    }else{
                        $this->ACT_layer_msg("修改姓名为".$member_info['$member_name'].'数据时出错！',8,$_SERVER['HTTP_REFERER'],2,1);
                    }
                }else{
                    continue;
                }
            }else{
                $this->ACT_layer_msg("数据有空值！请检查后重试！",8,$_SERVER['HTTP_REFERER'],2,1);
            }
        }
        $msg = "本次提交待入职人数为：".$num;
//        $value = '';
//        $value.="`entry_state`='2'";
//        $num = $this->obj->DB_select_num("entry","`job_id`='".$id."' AND `entry_state` = '0'");
//        $res = $this->obj->DB_update_all("entry",$value,"`job_id`='".$id."' AND `entry_state` = '0'");
//        $msg .= ",本次面试不合格人数为：".$num;
        $this->ACT_layer_msg($msg,9,$_SERVER['HTTP_REFERER'],2,1);
    }

    function xls_action(){
        include(CONFIG_PATH."db.data.php");
        $this->yunset("arr_data",$arr_data);
        if($_POST['pid']){
            if( $_POST['keyword'] ){
                $keyword = $_POST['keyword'];
                if( $_POST['entry_state'] ){
                    $list=$this->obj->DB_select_all("entry","`id` in (".$_POST["pid"].") and CONCAT(`member_name`,`id`) like '%".trim($keyword)."%' and `entry_state`= '".$_POST['entry_state']."'",'*');
                }else{
                    $list=$this->obj->DB_select_all("entry","`id` in (".$_POST["pid"].") and CONCAT(`member_name`,`id`) like '%".trim($keyword)."%'",'*');
                }
            }else{
                if( $_POST['entry_state'] ) {
                    $list = $this->obj->DB_select_all("entry", "`id` in (" . $_POST["pid"] . ")", '*');
                }else{
                    $list = $this->obj->DB_select_all("entry", "`id` in (" . $_POST["pid"] . ") and `entry_state`= '".$_POST['entry_state']."'", '*');
                }
            }
        }else{
            if( $_POST['keyword'] ){
                $keyword = $_POST['keyword'];
                if( $_POST['entry_state'] ){
                    $list=$this->obj->DB_select_all("entry","1 and CONCAT(`member_name`,`id`) like '%".trim($keyword)."%' and `entry_state`= '".$_POST['entry_state']."'",'*');
                }else{
                    $list=$this->obj->DB_select_all("entry","1 and CONCAT(`member_name`,`id`) like '%".trim($keyword)."%'",'*');
                }
            }else{
                if( $_POST['entry_state'] ) {
                    $list = $this->obj->DB_select_all("entry", "`entry_state`= '".$_POST['entry_state']."'", '*');
                }else{
                    $list=$this->obj->DB_select_all("entry","1",'*');
                }
            }

        }
        if(!empty($list)){
            foreach($list as $k=>$v){
                $list[$k]['member_sex']=$arr_data['sex'][$v['member_sex']];
            }
            $this->yunset("list",$list);
            $this->yunset($this->MODEL('cache')->GetCache(array('city','hy','com','job')));

            $this->MODEL('log')->admin_log("导出简历信息");
            header("Content-Type: application/vnd.ms-excel");
            header("Content-Disposition: attachment; filename=".date('Y-m-d H')."简历表.xls");
            $this->yuntpl(array('admin/entry_xls'));
        }
    }

    function add_action(){
        $this->yunset("id",$_GET['id']);
        $this->yuntpl(array('admin/add'));
    }

    function addS_action(){
        $id = (int)$_POST['id'];
        $JobM=$this->MODEL('job');
        $job=$JobM->GetComjobOne(array("id"=>(int)$id,'1'));
        $value = '';
        $value.="`company_id`='".$job['company_id']."',";
        $value.="`company_name`='".$job['com_name']."',";
        $value.="`job_id`='".$id."',";
        $value.="`job_name`='".$job['name']."',";
//        $value.="`member_id`='".$user_id_new['user_id']."',";
//        $value.="`nickname`='".$user_id_new['username']."',";
        $value.="`member_name`='".$_POST['username']."',";
        $value.="`member_phone`='".$_POST['mobile']."',";
        $value.="`member_card`='".$_POST['idcard']."',";
        $value.="`member_sex`='".$_POST['sex']."',";
        $value.="`entry_state`='".$_POST['entry_state']."',";
        $value.="`source`='2',";
        $value.="`add_time`='".time()."'";
        $res = $this->obj->DB_insert_once("entry",$value);
        $sql = "update `phpyun_company_job` set `jobhits` = `jobhits`+1 WHERE `id` = $id ";
        $resb = $this->db->query( $sql );
        $this->ACT_layer_msg("发布成功！",9,1);
    }
}
?>